using System.Collections;
using System.Collections.Generic;
using Prime31;
using UnityEngine;

public class DownloadObb : MonoBehaviour
{
	private string mainPath;

	private string expPath;

	private string patchPath;

	[SerializeField]
	private List<GameObject> allObjectsInFirstLoading = new List<GameObject>();

	public VersionSelector neutrinoSwitcher;

	private void Awake()
	{
	}

	private void TurnOnObjects()
	{
		StartCoroutine(CheckNeutrino());
	}

	private IEnumerator CheckNeutrino()
	{
		yield return StartCoroutine(VersionSelector.DownloadNeutrinoSignaturesCoroutine(this));
		neutrinoSwitcher.gameObject.SetActive(true);
		yield return null;
		foreach (GameObject tmpObject in allObjectsInFirstLoading)
		{
			tmpObject.SetActive(true);
		}
	}

	private void Start()
	{
		GooglePlayDownloader.publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqsXnLbQdmJI/MvDCtO+Z6/uvUMDfjFzlMfRE5O2o53p4QMEBSmKs3T9OW/5tta2BMHMlt4f2r6uj7KJYh93lgdkSjDMSDsk+ryG6865wsSukASUQamzXS0VFZjBwLqK9qTW6TS8KdeMLNGxQmgK4WQqV0+b+oW4n2T6UoCnX7JQegeCgtEt0mrg0cPk4kW1bfPKr11V2lPfnjBepUIpX9GSoPGXuYPAHVfxVDp0d9N3fhv8eLjhVMvM3kpr4hYD4gWxBkOQDkZ+m2jJFi/zdUehKyTqLvwm4PAPL1wL3GAkb3dfAh3ZgeflQ2ZbB8JEDy3/QK+4aveV/DbRPVTFVVwIDAQAB";
		expPath = GooglePlayDownloader.GetExpansionFilePath();
		if (expPath == null)
		{
			Debug.LogError("External storage is not available!");
			return;
		}
		mainPath = GooglePlayDownloader.GetMainOBBPath(expPath);
		Debug.Log("Main = ..." + ((mainPath != null) ? mainPath.Substring(expPath.Length) : " NOT AVAILABLE"));
		if (mainPath == null)
		{
			if (!EtceteraAndroid.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE"))
			{
				EtceteraAndroid.requestPermissions(new string[1] { "android.permission.WRITE_EXTERNAL_STORAGE" });
				StartCoroutine(WaitPermission());
			}
			else
			{
				StartCoroutine(FetchDownload());
			}
		}
		else
		{
			TurnOnObjects();
		}
	}

	private IEnumerator FetchDownload()
	{
		Debug.Log("FetchOBB");
		GooglePlayDownloader.FetchOBB();
		while (mainPath == null)
		{
			Debug.Log("Waiting MainPath: " + mainPath);
			yield return new WaitForSeconds(0.5f);
			mainPath = GooglePlayDownloader.GetMainOBBPath(expPath);
		}
		TurnOnObjects();
	}

	private IEnumerator WaitPermission()
	{
		while (!EtceteraAndroid.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE"))
		{
			yield return null;
		}
		StartCoroutine(FetchDownload());
	}
}
